package com.zzx.threekingdom.judge;

import java.util.ArrayList;

import com.zzx.threekingdom.card.Card;
import com.zzx.threekingdom.card.CardHeap;
import com.zzx.threekingdom.card.CardList;
import com.zzx.threekingdom.judge.Judge.IAskYesNo;
import com.zzx.threekingdom.judge.Judge.IPassCard;
import com.zzx.threekingdom.model.AbsPlayerModel;
import com.zzx.threekingdom.model.ActionInfo;


public class Harvest extends AskAll
{

	private ArrayList<CardList> harvestCardss = new ArrayList<CardList>();
	public void beginAsk(ActionInfo act, Runnable doneR)
	{
		actionInfo.clear();
		actionInfo.setCard(act.getCard());
		actionInfo.setActionName(act.getActionName());
		ArrayList<AbsPlayerModel> pms = Judge.judge.getLivePmFromeCurrentTurn();
		harvestCardss.add(CardHeap.cardHeap.drawPlayCards(pms.size()));
		beginAsk(pms, doneR);
	}

	@Override
	public void doAsk(final AbsPlayerModel pm, Runnable doneR)
	{
		IAskYesNo iAskYesNo = new IAskYesNo() { public void onResult(boolean yes) {
			if(yes == false)
			{
				IPassCard ipc = new IPassCard() { public void onGotCard(Card card) {
					if(card != null)
					{
						pm.addHandCards(new CardList().addCard(card));
						removeCard(card);
					}
					goOnAsk();
				}};
				pm.pickTargetOneCard(harvestCardss, ipc);
				
			}
			else
				goOnAsk();
		}};
		
		new AskImpeccable().beginAsk(null, actionInfo.getActionName(), pm, iAskYesNo);
	}
	
	private void removeCard(Card card)
	{
		if(card != null)
		{
			for(int i = 0; i < harvestCardss.size(); i++)
			{
				CardList cards = harvestCardss.get(i);
				if(cards.remove(card))
					break;
			}
		}
	}
}
